package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqFlocculationCraftSceneMapper;
import com.guofen.pojo.SjqFlocculationCraftScene;
import com.guofen.pojo.SjqFlocculationProduceCheck;
import com.guofen.service.sjq.SjqFlocculationCraftSceneService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SjqFlocculationCraftSceneServiceImpl implements SjqFlocculationCraftSceneService {

    @Autowired
    private SjqFlocculationCraftSceneMapper sjqFlocculationCraftSceneMapper;

    @Override
    public boolean addForm(String xuNing_time, String drug_pump_flow1, String mud_pump_flow1, String drug_pump_flow2,
                           String mud_pump_flow2, String drug_pump_flow3, String mud_pump_flow3, String drug_pump_flow4,
                           String mud_pump_flow4, String tank_liquid_level, String flocculant_number, String iron_number,
                           String recorder, String remarks) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();

        SjqFlocculationCraftScene sjqFlocculationCraftScene = new SjqFlocculationCraftScene();
//        boolean isExist = true;
//        if ((sjqFlocculationCraftScene = findForm()) == null) {
//            sjqFlocculationCraftScene = new SjqFlocculationCraftScene();
//            isExist = false;
//        }

        sjqFlocculationCraftScene.setDate(date);

        sjqFlocculationCraftScene.setXuningTime(xuNing_time);

        if (!drug_pump_flow1.equals(""))
            sjqFlocculationCraftScene.setDrugpumpflow1(Double.parseDouble(drug_pump_flow1));
        if (!mud_pump_flow1.equals(""))
            sjqFlocculationCraftScene.setMudpumpflow1(Double.parseDouble(mud_pump_flow1));
        if (!drug_pump_flow2.equals(""))
            sjqFlocculationCraftScene.setDrugpumpflow2(Double.parseDouble(drug_pump_flow2));
        if (!mud_pump_flow2.equals(""))
            sjqFlocculationCraftScene.setMudpumpflow2(Double.parseDouble(mud_pump_flow2));
        if (!drug_pump_flow3.equals(""))
            sjqFlocculationCraftScene.setDrugpumpflow3(Double.parseDouble(drug_pump_flow3));
        if (!mud_pump_flow3.equals(""))
            sjqFlocculationCraftScene.setMudpumpflow3(Double.parseDouble(mud_pump_flow3));
        if (!drug_pump_flow4.equals(""))
            sjqFlocculationCraftScene.setDrugpumpflow4(Double.parseDouble(drug_pump_flow4));
        if (!mud_pump_flow4.equals(""))
            sjqFlocculationCraftScene.setMudpumpflow4(Double.parseDouble(mud_pump_flow4));
        if (!tank_liquid_level.equals(""))
            sjqFlocculationCraftScene.setTankliquidlevel(Double.parseDouble(tank_liquid_level));
        if (!flocculant_number.equals(""))
            sjqFlocculationCraftScene.setFlocculantnumber(Double.parseDouble(flocculant_number));
        if (!iron_number.equals(""))
            sjqFlocculationCraftScene.setIronnumber(Double.parseDouble(iron_number));

        if (sjqFlocculationCraftScene.getRecorder() != null) {
            if (!Arrays.asList(sjqFlocculationCraftScene.getRecorder().split(" ")).contains(recorder))
                sjqFlocculationCraftScene.setRecorder(sjqFlocculationCraftScene.getRecorder()+" "+recorder);
        }
        else
            sjqFlocculationCraftScene.setRecorder(recorder);

        if (!remarks.equals(""))
            sjqFlocculationCraftScene.setRemarks(sjqFlocculationCraftScene.getRemarks()+"\n"+remarks);

        int result = sjqFlocculationCraftSceneMapper.insert(sjqFlocculationCraftScene);
//        if (!isExist)
//               result = sjqFlocculationCraftSceneMapper.insert(sjqFlocculationCraftScene);
//        else
//            result = sjqFlocculationCraftSceneMapper.updateByPrimaryKeySelective(sjqFlocculationCraftScene);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public SjqFlocculationCraftScene findForm() {
        //获取最新记录
        SjqFlocculationCraftScene sjqFlocculationCraftScene = sjqFlocculationCraftSceneMapper.findLast();
        System.out.println("最新记录" + sjqFlocculationCraftSceneMapper.findLast());

        return sjqFlocculationCraftScene;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqFlocculationCraftScene> list = sjqFlocculationCraftSceneMapper.selectAll();
        this.getDiffer(list);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqFlocculationCraftScene> list = sjqFlocculationCraftSceneMapper.selectByDate(startDate, endDate);
        this.getDiffer(list);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqFlocculationCraftSceneMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqFlocculationCraftScene record) {
        return sjqFlocculationCraftSceneMapper.updateByPrimaryKeySelective(record);
    }

    private void getDiffer(List<SjqFlocculationCraftScene> list){
        for(int i=1; i<list.size(); i++){
            list.get(i).setDifferDrugpumpflow1(list.get(i).getDrugpumpflow1() - list.get(i-1).getDrugpumpflow1());
            list.get(i).setDifferDrugpumpflow2(list.get(i).getDrugpumpflow2() - list.get(i-1).getDrugpumpflow2());
            list.get(i).setDifferDrugpumpflow3(list.get(i).getDrugpumpflow3() - list.get(i-1).getDrugpumpflow3());
            list.get(i).setDifferDrugpumpflow4(list.get(i).getDrugpumpflow4() - list.get(i-1).getDrugpumpflow4());
            list.get(i).setDifferMudpumpflow1(list.get(i).getMudpumpflow1() - list.get(i-1).getMudpumpflow1());
            list.get(i).setDifferMudpumpflow2(list.get(i).getMudpumpflow2() - list.get(i-1).getMudpumpflow2());
            list.get(i).setDifferMudpumpflow3(list.get(i).getMudpumpflow3() - list.get(i-1).getMudpumpflow3());
            list.get(i).setDifferMudpumpflow4(list.get(i).getMudpumpflow4() - list.get(i-1).getMudpumpflow4());
        }
    }
}
